//
//
//  Created by 小柠檬🍋 on 2025/9/7.
//

import SwiftUI

//API站点
struct ApiSitesView: View {
    @StateObject var viewModel = ApiSitesViewModel()
    
    var body: some View {
        ZStack{
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            } else {
                ZStack{
                    ScrollView {
                        Spacer(minLength: 10)
                        //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                        LazyVStack(spacing: 15) {
                            ForEach(viewModel.items,id: \.objectId) { item in
                                ApiSitesItemView(bean: item)
                            }
                            LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                                viewModel.getData()
                            }) {
                                LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing, hideNoMore: true)
                            }
                            .noMore(viewModel.noMore)
                            .preload(offset: 10)
                        }
                    }
                    .enableRefresh()
                    .scrollIndicators(.hidden) // 隐藏滚动条
                    .padding(.horizontal, 20)
                }
            }}
        .navigationTitle("API 站点")
            .frame(maxWidth: .infinity)
            .background(.bg)
        .onAppear{
            if !viewModel.isInit {
                viewModel.getData(true)
                viewModel.isInit = true
            }
        }
    }
}

//#Preview {
//    ChatListView()
//}

//定义预览提供者
struct ApiSitesView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: ApiSitesView())
    }
}
